const hre = require('hardhat');
/**
 * 调用 XLayerLaunchPad.createTokenLaunch
 * 用法示例:
 * LAUNCHPAD_ADDRESS=0xd9B7BDD6DbC80DaEc00fB0a6c920eA50C1eDF017 \
 * NAME=TestToken SYMBOL=TTK DESC="Demo token" WEBSITE=https://example.com \
 * TELEGRAM=https://t.me/demo TWITTER=https://twitter.com/demo TARGET_ETH=1 \
 * FEE_VALUE_ETH=0.02 npx hardhat run scripts/create-launch.js --network xlayerTerigon
 */
async function main(){
  const p = process.env;
  const launchPadAddr = p.LAUNCHPAD_ADDRESS;
  if(!launchPadAddr){ 
    throw new Error('请设置 LAUNCHPAD_ADDRESS 环境变量。示例: LAUNCHPAD_ADDRESS=0xd9B7BDD6DbC80DaEc00fB0a6c920eA50C1eDF017'); 
  }
  const name = p.NAME || 'DemoToken';
  const symbol = p.SYMBOL || 'DMO';
  const desc = p.DESC || 'Demo launch';
  const website = p.WEBSITE || '';
  const telegram = p.TELEGRAM || '';
  const twitter = p.TWITTER || '';
  const targetEthStr = p.TARGET_ETH || '1';
  const feeValueStr = p.FEE_VALUE_ETH || '0.02';
  const targetEth = Number(targetEthStr);
  const feeEth = Number(feeValueStr);
  if(isNaN(targetEth) || targetEth < 1 || targetEth > 100) throw new Error('TARGET_ETH 必须在 1~100');
  if(isNaN(feeEth) || feeEth < 0.01) throw new Error('FEE_VALUE_ETH 必须 >=0.01');
  console.log('=== Create Token Launch ===');
  console.log('Network:', hre.network.name);
  console.log('LaunchPad:', launchPadAddr);
  console.log('Name/Symbol:', name, symbol);
  console.log('Target ETH:', targetEthStr, 'Creation Fee ETH:', feeValueStr);
  const [signer] = await hre.ethers.getSigners();
  console.log('Caller:', await signer.getAddress());
  console.log('Caller Balance:', hre.ethers.formatEther(await signer.provider.getBalance(await signer.getAddress())));
  const launchPad = await hre.ethers.getContractAt('XLayerLaunchPad', launchPadAddr, signer);
  const nextIdBefore = await launchPad.nextLaunchId();
  console.log('nextLaunchId (before):', nextIdBefore.toString());
  const tx = await launchPad.createTokenLaunch(
    name,
    symbol,
    desc,
    website,
    telegram,
    twitter,
    hre.ethers.parseEther(targetEthStr),
    { value: hre.ethers.parseEther(feeValueStr) }
  );
  console.log('Submitted tx:', tx.hash);
  const receipt = await tx.wait();
  console.log('Mined block:', receipt.blockNumber);
  const nextIdAfter = await launchPad.nextLaunchId();
  const createdId = nextIdBefore;
  console.log('nextLaunchId (after):', nextIdAfter.toString());
  console.log('Created Launch ID:', createdId.toString());
  const info = await launchPad.getLaunchInfo(createdId);
  // info mapping per contract:
  // 0 tokenAddress,1 creator,2 name,3 symbol,4 description,5 targetAmount,
  // 6 raisedAmount,7 deadline,8 launched,9 cancelled,10 currentPrice,11 tokensSold,12 totalSupplyForSale
  console.log('\n=== Launch Info ===');
  console.log('Token Address:', info[0]);
  console.log('Creator:', info[1]);
  console.log('Name:', info[2], 'Symbol:', info[3]);
  console.log('Description:', info[4]);
  console.log('Target Amount (ETH):', hre.ethers.formatEther(info[5]));
  console.log('Raised Amount (ETH):', hre.ethers.formatEther(info[6]));
  console.log('Deadline (unix):', info[7].toString());
  console.log('Launched:', info[8], 'Cancelled:', info[9]);
  console.log('Current Price (ETH):', hre.ethers.formatEther(info[10]));
  console.log('Tokens Sold:', info[11].toString(), 'Total For Sale:', info[12].toString());
  console.log('\n完成: Launch 创建成功。');
}
main().catch(e=>{console.error(e);process.exit(1);});
